package com.kensure.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import co.kensure.frame.ResultInfo;
import co.kensure.frame.ResultRowInfo;
import co.kensure.frame.ResultRowsInfo;
import co.kensure.http.RequestUtils;
import co.kensure.mem.PageInfo;

import com.alibaba.fastjson.JSONObject;
import com.kensure.shike.chongzhi.model.SKUserInout;
import com.kensure.shike.chongzhi.query.SKInoutQuery;
import com.kensure.shike.chongzhi.service.SKUserInoutService;
import com.kensure.shike.user.service.SKUserService;

/**
 * 用户的充值、体现流水逻辑
 * 
 * @author fankaidi
 *
 */
@Controller
@RequestMapping(value = "inout")
public class SKInoutController {

	@Resource
	private SKUserInoutService sKUserInoutService;

	@Resource
	private SKUserService sKUserService;

	/**
	 * 会员提交支付会员费111
	 */
	@ResponseBody
	@RequestMapping(value = "savevip.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo savein(HttpServletRequest req, HttpServletResponse rep) {
		String spbill_create_ip = RequestUtils.getClientIp(req);
		JSONObject json = RequestUtils.paramToJson(req);
		String code = json.getString("code");
		SKUserInout prepay = sKUserInoutService.saveVip(spbill_create_ip,code);
		return new ResultRowInfo(prepay);
	}

	/**
	 * 提现保存, jine 金额  111
	 */
	@ResponseBody
	@RequestMapping(value = "saveout.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo saveout(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		Double jine = json.getDouble("jine");
		String spbill_create_ip = RequestUtils.getClientIp(req);
		sKUserInoutService.saveOut(jine, spbill_create_ip);
		return new ResultRowInfo();
	}
	/**
	 * 提现保存, 需要后台审核的接口
	 */
	@ResponseBody
	@RequestMapping(value = "saveout2.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo saveOut2(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		Double jine = json.getDouble("jine");
		String alipayImg = json.getString("alipayImg");
		sKUserInoutService.saveOut2(jine, alipayImg);
		return new ResultRowInfo();
	}
	
	
	
	
	/**
	 * 充值记录保存, jine 金额  111
	 */
	@ResponseBody
	@RequestMapping(value = "savein", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo saveIn(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		Double jine = json.getDouble("jine");
		String spbill_create_ip = RequestUtils.getClientIp(req);
		SKUserInout prepay = sKUserInoutService.saveIn(jine, spbill_create_ip);
		return new ResultRowInfo(prepay);
	}

	
	/**
	 * 根据商家或者试客获取充值列表
	 */
	@ResponseBody
	@RequestMapping(value = "chongzhilist.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo chongzhilist(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		Integer status = json.getInteger("status");
		PageInfo page = JSONObject.parseObject(json.toJSONString(), PageInfo.class);
		List<SKUserInout> list = sKUserInoutService.getInoutList(1, status, page);
		long count = sKUserInoutService.getInoutCount(1, status);
		return new ResultRowsInfo(list, count);
	}

	/**
	 * 根据商家或者试客获取提现列表
	 */
	@ResponseBody
	@RequestMapping(value = "tixianlist.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo tixianlist(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		Integer status = json.getInteger("status");
		PageInfo page = JSONObject.parseObject(json.toJSONString(), PageInfo.class);
		List<SKUserInout> list = sKUserInoutService.getInoutList(2, status, page);
		long count = sKUserInoutService.getInoutCount(2, status);
		return new ResultRowsInfo(list, count);
	}

	/**
	 * 管理员获取提现列表
	 */
	@ResponseBody
	@RequestMapping(value = "alltixianlist.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo alltixianlist(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		SKInoutQuery query = JSONObject.parseObject(json.toJSONString(), SKInoutQuery.class);
		PageInfo page = JSONObject.parseObject(json.toJSONString(), PageInfo.class);
		List<SKUserInout> list = sKUserInoutService.getAllInoutList(query, page);
		long count = sKUserInoutService.getAllInoutCount(query);
		return new ResultRowsInfo(list, count);
	}

	
	
	
	/**
	 * 会员利润核算
	 */
	@ResponseBody
	@RequestMapping(value = "hylrhs.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo hylrhs(HttpServletRequest req, HttpServletResponse rep) {
		Map<String, Object> huiyuan = sKUserInoutService.saleNum();
		List<Map<String, Object>> list = new ArrayList<>();
		list.add(huiyuan);
		return new ResultRowsInfo(list);
	}
	
	
	/**
	 * 提现通过
	 */
	@ResponseBody
	@RequestMapping(value = "tongguoout.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo tongguoout(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		Long id = json.getLong("id");
		sKUserInoutService.tongguoOut(id);
		return new ResultRowInfo();
	}

}
